package org.janon.httpproxy;

import javax.swing.JLabel;

public class Monitor implements Runnable
{
	private static Monitor monitor = null;

	private Thread thread = null;
	
	private JLabel label = null;

	private Monitor()
	{
	}

	public static Monitor getInstance()
	{
		if (monitor == null)
		{
			monitor = new Monitor();
		}
		return monitor;
	}

	public void run()
	{
		int temp = -1, temp1 = -1, num = -1, aCount = -1;
		while (true)
		{
			temp = Log.getInstance().getThreadNum();
			temp1 = Thread.activeCount();
			if (num != temp || aCount != temp1)
			{
				num = temp;
				aCount = temp1;
				if(label == null)
					Log.getInstance().writeLog("Client numbers = " + num + "  Threads: " + aCount);
				else
					label.setText("Client numbers = " + num + "  Threads: " + aCount);
			}

			try
			{
				Thread.sleep(3000);
			}
			catch (InterruptedException e)
			{
			}
		}
	}

	public void start()
	{
		thread = new Thread(this);
		thread.start();
	}

	@SuppressWarnings("deprecation")
	public void stop()
	{
		if (thread != null)
		{
			thread.stop();
		}
		thread = null;
		
		if(label != null)
			label.setText(" ");
	}
	
	public void setLabel(JLabel label)
	{
		this.label = label;
	}
}
